package com.nowcoder.community.controller;

import com.nowcoder.community.annotation.LoginRequired;
import com.nowcoder.community.constant.CommunityConstant;
import com.nowcoder.community.entity.Comment;
import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.entity.Event;
import com.nowcoder.community.event.EventProducer;
import com.nowcoder.community.service.CommentService;
import com.nowcoder.community.service.DiscussPostService;
import com.nowcoder.community.util.HostHolder;
import com.nowcoder.community.util.RedisKeyUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 花木凋零成兰
 * @date 2024/3/15 20:31
 */
@Controller
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    @Resource
    private HostHolder hostHolder;

    @Resource
    private EventProducer eventProducer;

    @Resource
    private DiscussPostService discussPostService;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 新增评论
     * @param discussPostId
     * @param comment
     * @return
     */
    @RequestMapping(path = "/add/{discussPostId}", method = RequestMethod.POST)
    @LoginRequired
    public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment) {
        comment.setUserId(hostHolder.getUser().getId());
        comment.setStatus(0);
        comment.setCreateTime(new Date());
        commentService.addComment(comment);

        // 触发评论事件
        // 构造事件信息
        Map<String, Object> data = new HashMap<>();
        data.put("postId", discussPostId);
        Event event = Event.builder()
                .topic(CommunityConstant.TOPIC_COMMENT)
                .userId(hostHolder.getUser().getId())
                .entityType(comment.getEntityType())
                .data(data)
                .build();
        if (comment.getEntityType() == CommunityConstant.ENTITY_TYPE_POST) {    // 给帖子的评论
            DiscussPost target = discussPostService.findDiscussPostById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());  // 确定事件涉及实体所属用户
        } else if (comment.getEntityType() == CommunityConstant.ENTITY_TYPE_COMMENT) {  // 给评论的评论
            Comment target = commentService.findCommentById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        }
        // 发送事件
        eventProducer.fireEvent(event);

        // 触发发帖事件 即当评论的对象为帖子时
        if (comment.getEntityType() == CommunityConstant.ENTITY_TYPE_POST) {
            // 触发事件
            event = Event.builder()
                    .topic(CommunityConstant.TOPIC_PUBLISH)
                    .userId(comment.getUserId())
                    .entityType(CommunityConstant.ENTITY_TYPE_POST)
                    .entityId(discussPostId)
                    .build();
            eventProducer.fireEvent(event); // 事件发布

            // 计算帖子的分数的key
            String redisKey = RedisKeyUtil.getPostScoreKey();
            // 存储到redis的Set中 去重 重复数据会被替换
            redisTemplate.opsForSet().add(redisKey, discussPostId);

        }

        return "redirect:/discuss/detail/" + discussPostId;
    }

}
